package com.lc.railway.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.railway.R;

import org.json.JSONArray;
import org.json.JSONObject;

import androidx.recyclerview.widget.RecyclerView;


/**
 * @Title：
 * @Description：
 * @Author：huqinghan(19044311)
 * @Date：2021/3/24
 */
public class TestnewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    JSONArray jsa;
    Context mContext;
    public TestnewAdapter(Context context){
        this.mContext = context;
    }

    public void setData(JSONArray jsa){
        this.jsa = jsa;
    }

    public int getItemIndexForGroup(int index){
        int num = 0;
        for (int i = 0; i < jsa.length(); i++) {
            if (isGroup(i)){
                num++;
            }
            if (num == index){
                return i;
            }
        }
        return 0;
    }

    public int getGroupIndex(int index){
        int num = 0;
        for (int i = 0; i < index; i++) {
            if (isGroup(i)){
                num++;
            }
        }
        return num;
    }


    public boolean isGroup(int position){
        JSONObject jo = jsa.optJSONObject(position);
        return jo.has("_isGroup") && jo.optBoolean("_isGroup");
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if (viewType == 0){
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_test_group, viewGroup, false);
            ItemHolder holder = new ItemHolder(view);
            holder.titile = view.findViewById(R.id.header_title);
            return holder;
        }else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_test_child, viewGroup, false);
            ItemChildHolder holder = new ItemChildHolder(view);
            holder.name = view.findViewById(R.id.goods);
            holder.headerTitle = view.findViewById(R.id.header_title);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ItemHolder) {
            ItemHolder holder = (ItemHolder) viewHolder;
            JSONObject jo = getItem(position);
            holder.titile.setText(jo.optString("classifyName"));
        } else if (viewHolder instanceof ItemChildHolder) {
            ItemChildHolder holder = (ItemChildHolder) viewHolder;
            JSONObject jo = getItem(position);
            holder.name.setText(jo.optString("commodityName"));
            String classifyName = jo.optString("classifyName");
            holder.headerTitle.setText(jo.optString("classifyName"));
            if (position != 0){
                JSONObject oldJo = getItem(position - 1);
                String oldClassifyName = oldJo.optString("classifyName");
                holder.headerTitle.setVisibility(classifyName.equals(oldClassifyName) ? View.GONE : View.VISIBLE);
            } else {
                holder.headerTitle.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isGroup(position)) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return jsa != null ? jsa.length() : 0;
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        TextView titile;

        public ItemHolder(View itemView) {
            super(itemView);
        }
    }

    class ItemChildHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView headerTitle;

        public ItemChildHolder(View itemView) {
            super(itemView);
        }
    }

    public JSONObject getItem(int position) {
        JSONObject jo = jsa.optJSONObject(position);
        return jo;
    }


}